package com.cfjjc.gz.common.action;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;

import com.cfjjc.gz.common.consts.SystemConst;
import com.cfjjc.gz.common.exception.OTSException;
import com.cfjjc.gz.common.service.LoginService;
import com.cfjjc.gz.usraccount.vo.A_USRVO;
import com.cfjjc.gz.common.listener.OnlineUserListener;
 

public class LoginAction extends PagedAction{
	
	@Autowired
	private LoginService loginService;
	
	private String validate; // 验证码
	
	public String getValidate() {
		return validate;
	}

	public void setValidate(String validate) {
		this.validate = validate;
	}

	// model对象
	private A_USRVO model = new A_USRVO();
	
	public A_USRVO getModel() {
		return model;
	}

	public void setModel(A_USRVO model) {
		this.model = model;
	}

	// 显示登录页面
	public String showLogin() throws Exception {
		return SUCCESS;
	}
	
	protected List rows = new ArrayList();
	
	public List getRows() {
		return rows;
	}

	public void setRows(List rows) {
		this.rows = rows;
	}
	
	
	// 登录成功页面
	public String showMain() {
		return SUCCESS;
	}

	// 查询用户的菜单权限
	public String showMenu() throws Exception {
		try {
			A_USRVO user = (A_USRVO) session.get(SystemConst.SESSION_USER);
			this.rows = loginService.queryUserLimit(user);
		} catch (OTSException e) {
			this.addActionError(e.getMessage());
			return ERROR;
		}
		return SUCCESS;
	}
	
	
	// 响应用户登录请求
	public String doLogin() throws Exception {

		// 登录,要保存在session中的用户对象
		A_USRVO user = null;
		try {
			
			if (validate == null || validate.trim().equals("")) {
				throw new OTSException("校验码不能为空！");
			}

			String session_validate = (String) session.get("rand");
			if (!validate.equals(session_validate)) {
				throw new OTSException("验证码不正确，请重新输入！");
			}

			System.out.println("********model.getLogin_name*************:"+model.getLogin_name());

			user = loginService.doUserLogin(this.model);

			if (SystemConst.YES_CONST.equals(user.getFirst_login())) {
				operationType = 99;
			} else {
				operationType = 30;
			}

			HttpSession onLineSession = (HttpSession) OnlineUserListener.sessionMap
					.get(user.getUsr_id());
			if (onLineSession != null) { // 当前用户在其他地方已经登录，踢出之前的会话信息
				String alreadySessionID = onLineSession.getId();
				String currentSessionID = request.getSession().getId();
				
				if (!alreadySessionID.equals(currentSessionID)) {
					logger.debug("统一登录名=[" + user.getLogin_name() + ", sessionId="
							+ onLineSession.getId()
							+ "]的用户已经在其他地方登录系统，系统现将其session踢掉！");
					onLineSession.invalidate();
			
				}
			}

			session.put(SystemConst.SESSION_USER, user);
			OnlineUserListener.sessionMap.put(user.getUsr_id(),
					request.getSession());

		} catch (OTSException e) {
			this.addActionError(e.getMessage());
			return ERROR;
		}
		return SUCCESS;
	}
	
	// 响应用户退出请求
	public String doLogout() throws Exception {

		A_USRVO user = (A_USRVO) session.get(SystemConst.SESSION_USER);

		if (user != null) {
			session = null; // 清除当前用户的session
			HttpSession onLineSession = (HttpSession) OnlineUserListener.sessionMap
					.get(user.getUsr_id());
			if (onLineSession != null) {
				onLineSession.invalidate(); // 清除session列表中当前用户的session
			}
			OnlineUserListener.sessionMap.remove(user.getUsr_id());
		}
		return SUCCESS;

	}
	
	
}
